package com.xmtlxx.oms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.oms.domain.bo.ContactsBo;
import com.xmtlxx.oms.domain.vo.ContactsVo;
import com.xmtlxx.oms.service.IContactsService;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 联系人Controller
 *
 * @author xmtlxx
 * @date 2022-08-12
 */
@Validated
@Api(value = "联系人控制器", tags = {"联系人管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/oms/contacts")
public class ContactsController extends BaseController {

    private final IContactsService iContactsService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<ContactsVo> initBean(){
        ContactsVo vo = new ContactsVo();
        vo.setDeptId(LoginHelper.getDeptId());
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询联系人列表
     */
    @ApiOperation("查询联系人列表")
    @SaCheckPermission("oms:contacts:list")
    @GetMapping("/list")
    public TableDataInfo<ContactsVo> list(@Validated(QueryGroup.class) ContactsBo bo, PageQuery pageQuery) {
        return iContactsService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询联系人列表
     */
    @ApiOperation("查询联系人列表")
    @GetMapping("/contacts")
    public R<List<ContactsVo>> contacts(@Validated(QueryGroup.class) ContactsBo bo) {
        return R.ok(iContactsService.queryList(bo));
    }

    /**
     * 查询联系人列表
     */
    @ApiOperation("查询联系人列表")
    @GetMapping("/contactsV2")
    public R<List<ContactsVo>> contactsV2(@Validated(QueryGroup.class) ContactsBo bo) {
        return R.ok(iContactsService.treeList(bo));
    }

    /**
     * 导出联系人列表
     */
    @ApiOperation("导出联系人列表")
    @SaCheckPermission("oms:contacts:export")
    @Log(title = "联系人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ContactsBo bo, HttpServletResponse response) {
        List<ContactsVo> list = iContactsService.queryList(bo);
        ExcelUtil.exportExcel(list, "联系人", ContactsVo.class, response);
    }

    /**
     * 获取联系人详细信息
     */
    @ApiOperation("获取联系人详细信息")
    @SaCheckPermission("oms:contacts:query")
    @GetMapping("/{id}")
    public R<ContactsVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iContactsService.queryById(id));
    }

    /**
     * 新增联系人
     */
    @ApiOperation("新增联系人")
    @SaCheckPermission("oms:contacts:add")
    @Log(title = "联系人", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ContactsVo vo) {
        ContactsBo bo = new ContactsBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iContactsService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改联系人
     */
    @ApiOperation("修改联系人")
    @SaCheckPermission("oms:contacts:edit")
    @Log(title = "联系人", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ContactsVo vo) {
        ContactsBo bo = new ContactsBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iContactsService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除联系人
     */
    @ApiOperation("删除联系人")
    @SaCheckPermission("oms:contacts:remove")
    @Log(title = "联系人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iContactsService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
